package com.bjwl.service.impl;
import com.bjwl.dao.SettlementMapper;
import com.bjwl.dao.OrdersMapper;
import com.bjwl.pojo.Settlement;
import com.bjwl.pojo.Business;
import com.bjwl.pojo.Orders;
import com.bjwl.pojo.dto.PrintingDto;
import com.bjwl.pojo.vo.AdminOrdersQueryVo;
import com.bjwl.pojo.vo.TimerQueryVo;
import com.bjwl.service.OrdersService;
import com.bjwl.utils.PageBean;
import org.springframework.test.annotation.Commit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: dzx
 * @Date:Create：in 2020/4/9 16:51
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    public OrdersMapper ordersMapper;
    @Autowired
    public SettlementMapper settlementMapper;

    @Override
    public boolean findOrderCardIdById(Integer id) {
        Integer cardid = ordersMapper.findOrderCardIdById(id);
        if(cardid == null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List<Orders> findOrdersByBstoreid(Integer branchstoreid) {
        return ordersMapper.findOrdersByBstoreid(branchstoreid);
    }

    @Override
    public Orders updateShowOrders(Integer oid) {
        return ordersMapper.updateShowOrders(oid);
    }

    @Override
    public void updateShowOrders(Orders orders) {
        ordersMapper.updateOrdersByid(orders);
    }

    @Override
    @Transactional
    public void deleteOrders(Integer oid) {
        ordersMapper.deleteOrders(oid);
    }
    /**
     * 总店显示所有派单，并标注所属分店，分页，条件查询
     * @param adminOrdersQueryVo
     * @return
     */
    @Override
    public PageBean<Orders> findAllOrders(AdminOrdersQueryVo adminOrdersQueryVo) {
        PageBean<Orders> pageBean = new PageBean<Orders>(adminOrdersQueryVo.getPageNum(),6,ordersMapper.getTotalRecord(adminOrdersQueryVo));
        pageBean.setData(ordersMapper.findAllOrders(pageBean,adminOrdersQueryVo));
        return pageBean;
    }

    @Override
    public void addOrders(Orders orders) {

        ordersMapper.addOrders(orders);
    }

    @Override
    public Integer finOrdersCount(Integer bsid) {
        return ordersMapper.finOrdersCount(bsid);
    }
    @Override
    public Integer findAllOrdersTotalRecord() {
        return ordersMapper.findAllOrdersTotalRecord();
    }
    //**********************************************
   @Override
    public PageBean<Orders> findWorkByBstoreid(Integer pageNum, Integer branchstoreid, Integer orstate, Integer orderid,String ordervalue , String wstarttime ,String wendtime,String servproject) {
        PageBean<Orders> pageBean = new PageBean<Orders>(pageNum,6,ordersMapper.orderPaging(branchstoreid,orstate,orderid,ordervalue,wstarttime,wendtime,servproject));
        pageBean.setData(ordersMapper.findWorkByBstoreid(pageBean,branchstoreid,orstate,orderid,ordervalue,wstarttime,wendtime,servproject));
        return pageBean;
    }



    @Override
    public Business addOrdersShowbusin(Integer buid) {
        return ordersMapper.addOrdersShowbusin(buid);
    }


    @Override
    @Transactional
    @Commit
    public void updateOrstate(Orders orders) {
        Orders orders2 = ordersMapper.updateShowOrders(orders.getOid());
        if(orders2.getOsstate()==0){
            ordersMapper.updateOrstate(orders);
            Orders orders1 = ordersMapper.updateShowOrders(orders.getOid());
            Integer cardid = ordersMapper.findCardidByOid(orders.getOid());
            Settlement settlement = new Settlement();
            settlement.setCardid(cardid);
            settlement.setServiceclass(orders1.getBusinessclass());
            settlement.setServicetime(orders1.getServiceTime());
            settlement.setCname(orders1.getcName());
            settlement.setSettlestate("未结算");
            settlement.setBsid(orders1.getBranchstoreid());
            //派单号
            Date dd=new Date();
            SimpleDateFormat sim=new SimpleDateFormat("yyyyMMddHHmmss");
            String oWriteTime=sim.format(dd);
            settlement.setOrdersid("PD-"+oWriteTime);
            settlementMapper.addSettlement(settlement);
        }else {
            ordersMapper.updateOrstate(orders);
        }

    }

    @Override
    public ArrayList<Orders> findOrdersJob(TimerQueryVo timerQueryVo) {
        return ordersMapper.findOrdersJob(timerQueryVo);
    }
    @Override
    public PrintingDto findPrintingById(Integer id) {
        return  ordersMapper.findPrintingById(id);
    }

    @Override
    public PrintingDto findPrintingByIdtwo(Integer id) {
        return ordersMapper.findPrintingByIdtwo(id);
    }
}
